// pages/demand-list/demand-list.js
const app = getApp()

Page({
  data: {
    demandList: [],
    page: 1,
    pageSize: 5,
    noMoreData: false,
    isLoading: false,
    searchKeyword: '',
    
    // 筛选条件
    currentFilter: { value: 'all', label: '全部服务' },
    filterOptions: [
      { id: 0, label: '全部服务', value: 'all' },
      { id: 1, label: '医院陪护', value: 'care' },
      { id: 2, label: '跑腿买药', value: 'errand' },
      { id: 3, label: '上门护理', value: 'nursing' }
    ],
    
    // 排序方式
    currentSortType: { id: 0, name: '智能排序' },
    sortTypes: [
      { id: 0, name: '智能排序' },
      { id: 1, name: '最新发布' },
      { id: 2, name: '预算最高' }
    ],

    isAuthorized: false
  },

  onLoad() {
    this.checkAuthStatus()
    this.loadData(true)
  },

  // 检查认证状态
  checkAuthStatus() {
	const self = this;
	this.isAuthorized = wx.getStorageSync('isAuthorized')
	if(!this.isAuthorized){
		app.util.request({
				url: 'User/index',
				data: {
						m: app.mdName
				},
				success(res) {
						var data = res.data.data;
						console.log(data);
						console.log(data.staff.status)
					   if(data.staff.status == '20'){
							self.setData({ 'isAuthorized':true })
							wx.setStorageSync('isAuthorized',true)
					   }
				}
		})
	}else{
		self.setData({ 'isAuthorized':true })
		wx.setStorageSync('isAuthorized',true)
	}
	// const isAuthorized = app.globalData.userInfo?.certStatus === 2
    // this.setData({ isAuthorized })
  },

  // 加载数据
  async loadData(init = false) {
    if (this.data.noMoreData && !init) return
    this.setData({ isLoading: true })

    var that = this
          	const { page, pageSize, searchKeyword, currentFilter } = this.data;
                app.util.request({
                        url: 'Xuqiu/xuqiulists',
                        data: {
                          keyword: searchKeyword,
                          type: currentFilter.value,
          				page: init ? 1 : page,
          				pageSize,
                        },
                        complete(res) {
                                that.setData({
                                        noMoreData: res.data.data.list.length < pageSize
                                });
                        },
                        success(res) {
                                var data = res.data.data;
          					  console.log(data)
          					  var list = data.list
                                that.setData({
                                      demandList: init ? list : [...this.data.demandList, ...list],
                                      page: init ? 2 : page + 1,
          							isLoading: false
                                });
                        }
                })
  },

  // 图片预览
  previewImage(e) {
    const current = e.currentTarget.dataset.url
    const urls = this.data.demandList
      .map(item => item.cover)
      .flat()
      .filter(url => url)
    
    wx.previewImage({
      current,
      urls
    })
  },

  // 切换服务类型
   changeServiceType(e) {
     const index = e.detail.value
     this.setData({ currentServiceType: this.data.serviceTypes[index] })
     this.loadData(true)
   },
 
   // 切换排序方式
   changeSortType(e) {
     const index = e.detail.value
     this.setData({ currentSortType: this.data.sortTypes[index] })
     this.loadData(true)
   },
 
   // 跳转详情页
   navigateToDetail(e) {
     const id = e.currentTarget.dataset.id
	 console.log(id)
     wx.navigateTo({
       url: `/vp_pz/pages/xqfb/detail?id=`+id
     })
   },
 
   // 跳转发布页
   navigateToPublish() {
     wx.navigateTo({
       url: '/vp_pz/pages/xqfb/fabu'
     })
   },
 
   // 跳转认证页
   navigateToAuth() {
     wx.navigateTo({
       url: '/vp_pz/pages/staff/reg'
     })
   },
 
   // 加载更多
   loadMore() {
     if (!this.data.noMoreData) {
       this.loadData()
     }
   },

})